//引入gulp和gulp插件
var gulp = require('gulp');
var runSequence = require('run-sequence');
var rev = require('gulp-rev');
var revCollector = require('gulp-rev-collector');

//定义css、js源文件路径
var cssSrc = './app/css/*.css';
var jsSrc = './app/js/*.js';


//CSS生成文件hash编码并生成 rev-manifest.json文件名对照映射
gulp.task('revCss', function() {
    return gulp.src(cssSrc)
        .pipe(rev())
        .pipe(gulp.dest('./dist/css'))
        .pipe(rev.manifest())
        .pipe(gulp.dest('./rev/css'));
});


//js生成文件hash编码并生成 rev-manifest.json文件名对照映射
gulp.task('revJs', function() {
    return gulp.src(jsSrc)
        .pipe(rev()) //给文件添加hash编码
        .pipe(gulp.dest('./dist/js'))
        .pipe(rev.manifest()) //生成rev-mainfest.json文件作为记录
        .pipe(gulp.dest('./rev/js'));
});


//Html替换css、js文件版本
gulp.task('revHtmlCss', function() {
    return gulp.src(['./rev/css/*.json', './app/*.html'])
        .pipe(revCollector()) //替换html中对应的记录
        .pipe(gulp.dest('./dist')); //输出到该文件夹中
});
gulp.task('revHtmlJs', function() {
    return gulp.src(['./rev/js/*.json', './dist/*.html'])
        .pipe(revCollector())
        .pipe(gulp.dest('./dist'));
});


//开发构建
gulp.task('dev', function(done) {
    condition = false;
    //依次顺序执行
    runSequence(
        ['revCss'],
        ['revHtmlCss'],
        ['revJs'],
        ['revHtmlJs'],
        done);
});